<template>
  <TitleBar :value="buttons"
            :closable="!isBack"
            :back="isBack"
            v-bind="$attrs"
            @click="handleClick" />
</template>

<script>
import OrderMixin, { STATUS_CODES } from "./order.mixin";
import TitleBar from "@/components/basic/TitleBar.vue";
import { mapState } from "vuex";
export default {
  name: "OrderTitleBar",
  components: { TitleBar },
  mixins: [OrderMixin],
  inheritAttrs: true,
  props: {
    value: Object,
    isAll: [Boolean, Number],
    isBack: Boolean
  },
  data () {
    return {
      // fromPath: null,
    };
  },
  computed: {
    ...mapState({
      tenantInfo: (state) => state.tenant.tenantInfo
    }),
    printTemplate () {
      return this.tenantInfo?.config?.printTemplate || [];
    },
    echo () {
      return this.order?.echo;
    },
    // 租户配置打印路由
    printPath () {
      const statusPrint = {
        20: 'orderSignTemplate',
        30: 'orderReceiptTemplate',
        40: 'orderReceiptedTemplate'
      }
      let path = null;
      let printName = statusPrint[this.status];
      this.printTemplate?.length && this.printTemplate.some((o) =>
        (path = printName === o.name ? o.value.split(";") : null)
      );
      if (this.echo && this.echo?.orderTypeName) {
        const echoPath = {
          "OEM直发": "TopThroughPrint",
          "干线直发": "TopTrunkLinePrint",
          "经销商直发": "TopDistributorPrint",
        }
        return path.length && path.includes(echoPath[this.echo?.orderTypeName]) ? echoPath[this.echo?.orderTypeName] : null;
      } else {
        return path.length ? path[0] : null;
      }
    },
    // 有效打印路由
    effectivePaths () {
      return ['OrderPrint', 'TopThroughPrint', 'TopTrunkLinePrint', 'TopDistributorPrint'];
    },
    orderId () {
      return Number(this.order?.id || this.order?.orderId);
    },
    status () {
      return Number(this.order?.status);
    },
    showPrintButton () {
      return (
        this.status === STATUS_CODES.WAIT_SIGN ||
        this.status === STATUS_CODES.WAIT_RECEIPT ||
        this.status === STATUS_CODES.COMPLETED
      );
    },
    buttons () {
      return [
        {
          title: "回单",
          fulfill: this.handleReceipt,
          type: "primary",
          hidden: this.status !== 30 || this.isAll
        },
        {
          title: "签收",
          fulfill: this.handleSign,
          type: "primary",
          hidden: this.status !== 20 || this.isAll
        },
        {
          title: "打印",
          plain: true,
          hidden: !this.showPrintButton,
          fulfill: this.handlePrint
        }
      ];
    }
  },
  methods: {
    handleClick (button) { },
    handleSign () {
      this.$emit("sign");
    },
    handleReceipt () {
      this.$emit("receipt");
    },
    handlePrint () {
      const { href } = this.$router.resolve({
        name: this.effectivePaths.includes(this.printPath) ? this.printPath : "OrderPrint",
        query: {
          orderId: this.orderId
        }
      });
      window.open(href, "_blank");
    }
  }
};
</script>
